package com.palmshop.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message.RecipientType;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * 用户邮件激活链接和密码重置相关功能的工具类
 * @author quan
 *
 */
public class EmailUtils {

    private static final String FROM = "qxianx@163.com";  
    
    /** 
     * 注册成功后,向用户发送帐户激活链接的邮件 
     * @param user 未激活的用户 
     * @throws IOException 
     * @throws FileNotFoundException 
     */  
    public static void sendAccountActivateEmail(String logName,String email,String activeCode) throws FileNotFoundException, IOException {  
        //Session session = getSession();  
        final Properties props = new Properties();
		//通过文件流读取并解析配置文件内容
		props.load(
			new FileInputStream("src/main/resources/config.properties")
		);
		final String pwd=props.getProperty("password");
        Session session = Session.getInstance(props, new Authenticator() {  
            @Override  
            protected PasswordAuthentication getPasswordAuthentication() {  
                String password = pwd;  
                return new PasswordAuthentication(props.getProperty("logname"), password);  
            }  
              
        });  
        MimeMessage message = new MimeMessage(session);  
        try {  
            message.setSubject("帐户激活邮件");  
            message.setSentDate(new Date());  
            message.setFrom(new InternetAddress(props.getProperty("logname")));  
            message.setRecipient(RecipientType.TO, new InternetAddress(email));  
            message.setContent("<a href='" + props.getProperty("ipport")+"/AccountActivate/activateAccount?logName="   
                    + logName + "&" + "checkCode" + "=" + activeCode+"'>点击激活帐户</a>","text/html;charset=utf-8");  
            // 发送邮件  
            Transport.send(message);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
      
    /** 
     * 发送重设密码链接的邮件 
     * @throws IOException 
     * @throws FileNotFoundException 
     */  
    public static void sendResetPasswordEmail(String user,String email) throws FileNotFoundException, IOException {  
        Session session = getSessionT();  
        MimeMessage message = new MimeMessage(session);  
        try {  
            message.setSubject("找回您的帐户与密码");  
            message.setSentDate(new Date());  
            message.setFrom(new InternetAddress(FROM));  
            message.setRecipient(RecipientType.TO, new InternetAddress(email));  
            message.setContent("您的新密码为 :"+user+", 请注意保存！<br/>","text/html;charset=utf-8");  
            // 发送邮件  
            Transport.send(message);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
      
    public static Session getSession() throws FileNotFoundException, IOException {  
        Properties props = new Properties();
		//通过文件流读取并解析配置文件内容
		props.load(
			new FileInputStream("src/main/resources/config.properties")
		);
		final String pwd=props.getProperty("password");
        Session session = Session.getInstance(props, new Authenticator() {  
            @Override  
            protected PasswordAuthentication getPasswordAuthentication() {  
                String password = pwd;  
                return new PasswordAuthentication(FROM, password);  
            }  
              
        });  
        return session;  
    }  
    
    public static Session getSessionT() {  
        Properties props = new Properties();  
        props.setProperty("mail.transport.protocol", "smtp");  
        props.setProperty("mail.smtp.host", "smtp.163.com");  
        props.setProperty("mail.smtp.port", "25");  
        props.setProperty("mail.smtp.auth", "true");  
        Session session = Session.getInstance(props, new Authenticator() {  
            @Override  
            protected PasswordAuthentication getPasswordAuthentication() {  
                String password = "714591q";  
//                InputStream is = EmailUtils.class.getResourceAsStream("password.dat");  
//                byte[] b = new byte[1024];  
//                try {  
//                    int len = is.read(b);  
//                    password = new String(b,0,len);  
//                } catch (IOException e) {  
//                    e.printStackTrace();  
//                }  
                return new PasswordAuthentication(FROM, password);  
            }  
              
        });  
        return session;  
    }  
    
    
    public static void main(String[] args) throws FileNotFoundException, IOException {
//		sendAccountActivateEmail("","331562046@qq.com","ss");
    	String user="name";
    	String email="750137847@qq.com";
		sendResetPasswordEmail( "4545464654", email);
	}
} 
